package A一周刷爆LeetCode.A基础班.B认识复杂度和简单排序算法;

/**
 * @author wei.zhao
 * @description: 在一个有序数组中，找>=某个数最左侧的位置
 * @date 2022年01月10日 16:36
 */
public class Code05_BSNearLeft {

    public static int nearestIndex(int[] arr, int value) {
        int index = -1;
        if (null == arr || arr.length == 0 || arr[arr.length - 1] < value) {
            return index;
        }
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = left + ((right - left) >> 1);
            if (arr[mid] >= value) {
                if (mid == 0 || arr[mid - 1] < value) {
                    return mid;
                } else {
                    right = mid - 1;
                }
            } else if (arr[mid] < value) {
                left = mid + 1;
            }
        }
        return index;
    }

}
